/**
 * Copyright (C) 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package psn.shan.demo.xml.enhancedvalidator;

import java.io.InputStream;

import javax.inject.Inject;
import javax.inject.Named;

import lombok.extern.slf4j.Slf4j;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import psn.shan.xml.enhancedvalidator.EnhancedValidator;
import psn.shan.xml.enhancedvalidator.ValidationException;

/**
 * PersonValidator <br />
 * 
 * Skeleton aspect to demonstrate validation as a cross-cutting concern.
 * 
 * @author Shan
 * 
 */
@Slf4j
@Named
@Aspect
public class PersonValidator {

	//	Validator specific for the Person Data.
	@Inject
	private EnhancedValidator<PersonRuleSet> mEnhancedValidator;

	// String Literals
	private static final InputStream PERSON_XSD = PersonValidator.class.getClassLoader().getResourceAsStream("PersonData.xsd");
	
	/**
	 * The pointcut to match any mock service method in the mock service object.
	 */
	@Pointcut("execution(* psn.shan.demo.xml.enhancedvalidator.PersonService.*(..))")
	public void personOperation() {}
	
	/**
	 * To show how validation errors can be globally logged and execution control passed along to the invoker. 
	 * 
	 * @param vex
	 * @throws ValidationException
	 */
	@AfterThrowing(pointcut="personOperation()", throwing="vex") 
	public void logValidationFailure(ValidationException vex) throws ValidationException {
		log.error("Because of validation problems, did not allow invocation of PersonService.");
		throw vex;
	}
	
	/**
	 * Advice to enforce validations on the mock service method pointcut.
	 * 
	 * @param aJoinPoint	The proceeding join point that encapsulates the mock business service method
	 * @throws Throwable
	 */
	@Around("personOperation()")
	public void validate(ProceedingJoinPoint aJoinPoint) throws Throwable {
		log.info("Applying advice for validation ...");

		// Expecting first argument to any of the mock service methods to be the XML document to be validated.
		InputStream lPersonXML = (InputStream) aJoinPoint.getArgs()[0];
		
		log.debug("XML is " + lPersonXML);
		log.debug("XSD is " + PERSON_XSD);
		
		// Invoke validation, any validation issues resulting in exceptions are propagated.
		mEnhancedValidator.validate(lPersonXML, PERSON_XSD);
				
		// If execution reaches this point, the document is valid and mock business service method can proceed.
		aJoinPoint.proceed();
	}
}
